package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.vo.YhOrderVo;
import com.ruoyi.system.kit.core.enums.OrderStateEnums;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.entity.YhOrder;
import com.ruoyi.system.service.IYhOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员购买订单Controller
 * 
 * @author ruoyi
 * @date 2023-06-06
 */
@Api(tags = "会员购买订单管理")
@RestController
@RequestMapping("/yh/order")
public class YhOrderController extends BaseController
{
    @Autowired
    private IYhOrderService yhOrderService;

    /**
     * 查询会员购买订单列表
     */
    @ApiOperation("获取会员购买订单列表")
    @PreAuthorize("@ss.hasPermi('yh:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhOrder yhOrder)
    {
        startPage();
        List<YhOrderVo> list = yhOrderService.orderList(yhOrder);
        return getDataTable(list);
    }

    /**
     * 会员购买订单总金额
     */
    @ApiOperation("会员购买订单总金额")
    @GetMapping("/totalRevenue")
    public AjaxResult totalRevenue(YhOrder yhOrder)
    {
        QueryWrapper<YhOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_status", OrderStateEnums.PAID.name());
        List<YhOrder> list = yhOrderService.list(queryWrapper);
        Double reduce = list.stream().map(YhOrder::getFlowPrice).reduce(0D, Double::sum);
        return AjaxResult.success(reduce);
    }

    /**
     * 导出会员购买订单列表
     */
    @ApiOperation("导出会员购买订单列表")
    @PreAuthorize("@ss.hasPermi('yh:order:export')")
    @Log(title = "会员购买订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhOrder yhOrder)
    {
        List<YhOrder> list = yhOrderService.list(yhOrder.queryWrapper());
        ExcelUtil<YhOrder> util = new ExcelUtil<YhOrder>(YhOrder.class);
        util.exportExcel(response, list, "会员购买订单数据");
    }

    /**
     * 获取会员购买订单详细信息
     */
    @ApiOperation("获取会员购买订单详细")
    @ApiImplicitParam(name = "id", value = "会员购买订单ID", required = true, dataType = "Long", paramType = "path")
    @PreAuthorize("@ss.hasPermi('yh:order:list')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yhOrderService.getById(id));
    }

    /**
     * 新增会员购买订单
     */
    @ApiOperation("新增会员购买订单")
    @Log(title = "会员购买订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhOrder yhOrder)
    {
        return success(yhOrderService.saveOrder(yhOrder));
    }

    /**
     * 修改会员购买订单
     */
    @ApiOperation("更新会员购买订单")
    @PreAuthorize("@ss.hasPermi('yh:order:edit')")
    @Log(title = "会员购买订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhOrder yhOrder)
    {
        return toAjax(yhOrderService.updateById(yhOrder));
    }

    /**
     * 删除会员购买订单
     */
    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "ids", value = "会员购买订单ID集合", required = true, dataType = "Long", paramType = "path")
    @PreAuthorize("@ss.hasPermi('yh:order:remove')")
    @Log(title = "会员购买订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(yhOrderService.removeByIds(ids));
    }
}
